package com.sinothk.special.buyer.userblog.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sinothk.special.buyer.userblog.entity.UserBlogCollector;
import com.sinothk.special.buyer.userblog.service.IUserBlogCollectorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

 /**
 * @Description: 个人博客收藏者
 * @Author: Liangyt
 * @Date:   2022-06-06
 * @Version: V1.0
 */
@Api(tags="个人博客收藏者")
@RestController
@RequestMapping("/blog/userBlogCollector")
@Slf4j
public class UserBlogCollectorController {

	@Autowired
	private IUserBlogCollectorService userBlogCollectorService;
	
	/**
	 * 分页列表查询
	 *
	 * @param userBlogCollector
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "个人博客收藏者-分页列表查询")
	@ApiOperation(value="个人博客收藏者-分页列表查询", notes="个人博客收藏者-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(UserBlogCollector userBlogCollector,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<UserBlogCollector> queryWrapper = QueryGenerator.initQueryWrapper(userBlogCollector, req.getParameterMap());
		Page<UserBlogCollector> page = new Page<UserBlogCollector>(pageNo, pageSize);
		IPage<UserBlogCollector> pageList = userBlogCollectorService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param userBlogCollector
	 * @return
	 */
	@AutoLog(value = "个人博客收藏者-添加")
	@ApiOperation(value="个人博客收藏者-添加", notes="个人博客收藏者-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody UserBlogCollector userBlogCollector) {
		userBlogCollectorService.save(userBlogCollector);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param userBlogCollector
	 * @return
	 */
	@AutoLog(value = "个人博客收藏者-编辑")
	@ApiOperation(value="个人博客收藏者-编辑", notes="个人博客收藏者-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody UserBlogCollector userBlogCollector) {
		userBlogCollectorService.updateById(userBlogCollector);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "个人博客收藏者-通过id删除")
	@ApiOperation(value="个人博客收藏者-通过id删除", notes="个人博客收藏者-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		userBlogCollectorService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "个人博客收藏者-通过id查询")
	@ApiOperation(value="个人博客收藏者-通过id查询", notes="个人博客收藏者-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		UserBlogCollector userBlogCollector = userBlogCollectorService.getById(id);
		if(userBlogCollector==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(userBlogCollector);
	}

	 /**
	  *   添加
	  *
	  * @param blogId
	  * @return
	  */
	 @AutoLog(value = "个人博客收藏者-添加")
	 @ApiOperation(value="个人博客收藏者-添加", notes="个人博客收藏者-添加")
	 @GetMapping(value = "/addCollectNum")
	 public Result<?> addCollectNum(@RequestParam(name="blogId") String blogId) throws Exception {
		 userBlogCollectorService.addCollectNum(blogId);
		 return Result.OK("添加成功！");
	 }
}
